﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_YPRK : Form
    {
        public Form_YPRK()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;
        BindingSource bs;
        SqlCommandBuilder builder;
        

        private void Form_YPRK_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            sda = new SqlDataAdapter("SELECT RKDH,RQ,RKLX,GYSBM,LRRY,JZRY,SHRY,JZRQ,BZ,HasJZ FROM ML_RKD0", con);
            builder = new SqlCommandBuilder(sda);
            ds = new DataSet();
            bs = new BindingSource();
            dgvYPRK.AutoGenerateColumns = true;
            try
            {
                sda.Fill(ds);
                dgvYPRK.DataSource = ds.Tables[0];
                dgvYPRK.Columns[0].HeaderText = "入库单号";
                dgvYPRK.Columns[1].HeaderText = "制单日期";
                dgvYPRK.Columns[2].HeaderText = "入库类型";
                dgvYPRK.Columns[3].HeaderText = "供应商/退药科室";
                dgvYPRK.Columns[4].HeaderText = "录入";
                dgvYPRK.Columns[5].HeaderText = "审核";
                dgvYPRK.Columns[6].HeaderText = "记账";
                dgvYPRK.Columns[7].HeaderText = "记账日期";
                dgvYPRK.Columns[8].HeaderText = "备注";
                dgvYPRK.Columns[9].HeaderText = "状态";
                bs.DataSource = ds.Tables[0];
                bNavigator.BindingSource = bs;
                dgvYPRK.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvYPRK.ClearSelection();
            dgvYPRK.Rows[0].Selected = true;
            dgvYPRK.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvYPRK.SelectedRows[0].Index - 1 + dgvYPRK.RowCount) % dgvYPRK.RowCount;
            dgvYPRK.ClearSelection();
            dgvYPRK.Rows[rowindx].Selected = true;
            dgvYPRK.FirstDisplayedScrollingRowIndex = dgvYPRK.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvYPRK.SelectedRows[0].Index + 1) % dgvYPRK.RowCount;
            dgvYPRK.ClearSelection();
            dgvYPRK.Rows[rowIndex].Selected = true;
            dgvYPRK.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvYPRK.FirstDisplayedScrollingRowIndex)
            {
                dgvYPRK.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvYPRK.Rows[rowIndex].Displayed)
            {
                dgvYPRK.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvYPRK.ClearSelection();
            dgvYPRK.Rows[dgvYPRK.RowCount - 2].Selected = true;
            dgvYPRK.FirstDisplayedScrollingRowIndex = dgvYPRK.RowCount - 1;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            dgvYPRK.ClearSelection();
            dgvYPRK.Rows[dgvYPRK.NewRowIndex].Selected = true;
            dgvYPRK.FirstDisplayedScrollingRowIndex = dgvYPRK.NewRowIndex;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该记录？", "请选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dgvYPRK.Rows.RemoveAt(this.dgvYPRK.CurrentRow.Index);
            }
        }

        private void bindingNavigatorEnterItem_Click(object sender, EventArgs e)
        {
            try
            {
                int rkdh = Convert.ToInt32(this.dgvYPRK.CurrentRow.Cells[0].Value);
                Form_YPRKD YPRKD = new Form_YPRKD(rkdh);
                YPRKD.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void bindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            dgvYPRK.CurrentCell = null;
            try
            {
                if(ds.HasChanges())
                    sda.Update (ds);
                MessageBox.Show("保存成功");
                //MessageBox.Show("保存成功，已保存" + sda.Update(ds).ToString() + "条修改记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException ex)
            {
                if (ex.Number == 2627)
                    MessageBox.Show("保存失败，编码必须是唯一的，请查看是否存在重复的编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else if (ex.Number == 515)
                    MessageBox.Show("保存失败，编码不能为空，请查看是否存在记录缺填编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("保存失败，网络故障，请稍后再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgvYPRK_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int rkdh = Convert.ToInt32(this.dgvYPRK.CurrentRow.Cells[0].Value);
                Form_YPRKD YPRKD = new Form_YPRKD(rkdh);
                YPRKD.ShowDialog();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("请先增加入库单！");
                MessageBox.Show(ex.ToString());
            }
        }

        private void bindingNavigatorAddNewItem_Click_1(object sender, EventArgs e)
        {
            int rkdh = Convert.ToInt32(this.dgvYPRK.CurrentRow.Cells[0].Value)+1;
            Form_YPRKD YPRKD = new Form_YPRKD(rkdh);
            YPRKD.ShowDialog();
        }
    }
}
